package com.odp.controller;

import com.odp.entity.*;
import com.odp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.util.List;
@CrossOrigin(origins = "http://localhost:5173")
@RestController
@RequestMapping("community")
public class CommunityController {
    @Autowired
    private PostService postService;


    @Autowired
    private SubscribeService subscribeService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ReplyService replyService;
    /*
        社区页面
     */
    @PostMapping("/test")
    public void test(){
        System.out.println("111");
    }



    //初始化社区模块展示的帖子们
    @GetMapping("/getPosts")
    public Result getPost(){
        List<PostDto> posts = postService.getPosts();
        if(posts!=null){
            return new Result(Code.GET_POST_OK,"获取需要展示的30个帖子成功",posts);
        }
        return new Result(Code.GET_POST_OK,"获取帖子失败",null);
    }
    /*
    点击发帖按钮  进入发帖页面   填写相关信息后  点击提交
                        执行这个方法
    发帖成功   对话框消失
     */
    @PostMapping("/post")
    public Result makePost(@RequestBody Post post){
        //真正需要的 标题  偏好  内容  图片   session里面拿当前登录的用户id
        if(postService.makePost(post))
        {
            return new Result(Code.ADD_POST_OK,"发帖成功",null);
        }
            return new Result(Code.ADD_POST_FAIL,"发帖失败",null);
    }

    //发帖界面,用户上传一张图片时，el-upload会请求这个地址，
    @PostMapping("/post/handlePicture")
    public Result handlePicture(){
        System.out.println("图片上传到这里处理");
        System.out.println("@@");
        return new Result(Code.ADD_POST_FAIL,"发帖失败",null);
    }

    //点击关注

    @PutMapping("/subscribe/{id}")
    //此id为被关注的人的id
    public Result subscribeUser(@PathVariable int id){
        //传入参数  被订阅用户id  session里面拿当前登录的用户id
        if(subscribeService.addSubscribe(id)){
            return new Result(Code.SUBSCRIBE_OK,"关注成功",null);
        }
        return new Result(Code.SUBSCRIBE_FAIL,"关注失败,已经关注过该用户",null);
    }

    //点击取关
    @DeleteMapping("/unSubscribe/{id}")
    public Result unSubscribeUser(@PathVariable int id){
        if(subscribeService.unSubscribe(id)){
            return new Result(Code.UNSUBSCRIBE_OK,"取消关注成功",null);
        }
        return new Result(Code.UNSUBSCRIBE_FAIL,"取消关注失败，请勿频繁点击",null);
    }

    //点赞帖子  进入帖子后点赞帖子
    //传入参数 帖子id
    @PutMapping("/like/post/{postId}")
    public Result likePost(@PathVariable int postId){
        if(postService.likePost(postId)){
            return new Result(Code.LIKE_OK,"点赞帖子成功",null);
        }
        return new Result(Code.LIKE_FAIL,"点赞帖子失败",null);
    }

    //点击不感兴趣  减少推荐
    @PutMapping("/unlike/{postId}")
    public Result unlike(@PathVariable int postId){
        postService.unlike(postId);
        return new Result(Code.UNLIKE_OK,"不感兴趣",null);
    }

    /*
        进入帖子
     */

    //初始化帖子的详情头部
    @GetMapping("/postDetailHead/{postId}")
    public Result getPostDetailHead(@PathVariable int postId){
        PostDetailHeadDto postDetailHead = postService.getPostDetailHead(postId);
        if(postDetailHead!=null){
            return new Result(Code.GET_POSTDETAIL_HEAD_OK,"成功获取帖子的详情（头部）",postDetailHead);
        }
        return new Result(Code.GET_POSTDETAIL_HEAD_FAIL,"获取帖子详情（头部）失败",null);
    }
    //初始化帖子的详情的content （包括每层楼及每楼的回复）
    @GetMapping("/postDetailContent/{postId}")
    public Result getPostDetailContent(@PathVariable int postId){
        List<PostDetailContentDto> postDetailContent = postService.getPostDetailContent(postId);
        if(postDetailContent!=null){
            return new Result(Code.GET_POSTDETAIL_CONTENT_OK,"获取帖子详情的content成功",postDetailContent);
        }
        return new Result(Code.GET_POSTDETAIL_CONTENT_FAIL,"获取帖子详情的content失败",null);
    }


    //点击关注按钮，关注帖主  同上

    //点赞帖子 同上

    //点赞楼层
    @PutMapping("/like/comment/{commentId}")
    public Result likeComment(@PathVariable int commentId){
        if(commentService.likeComment(commentId)){
            return new Result(Code.LIKE_OK,"点赞楼层成功",null);
        }
        return new Result(Code.LIKE_FAIL,"点赞楼层失败",null);
    }

    //点赞楼中楼
    @PutMapping("like/reply/{replyId}")
    public Result likeReply(@PathVariable int replyId){
        if(replyService.likeReply(replyId)){
            return new Result(Code.LIKE_OK,"点赞楼中楼成功",null);
        }
        return new Result(Code.LIKE_FAIL,"点赞楼中楼失败",null);
    }


    //评论
    @PostMapping("/comment")
    public Result makeComment(@RequestBody Comment comment){
        if(commentService.makeComment(comment)){
            return new Result(Code.COMMENT_OK,"评论成功",null);
        }
        return new Result(Code.COMMENT_FAIL,"评论失败",null);
    }


    //回复楼层/楼中楼  （点击回复弹出对话框）
    @PostMapping("/reply")
    public Result makeReply(@RequestBody Reply reply){
        if(replyService.makeReply(reply)){
            return new Result(Code.REPLY_OK,"回复成功",null);
        }
        return new Result(Code.REPLY_FAIL,"回复失败",null);
    }


}
